<template>
  <el-row type="flex">
    <el-date-picker
      v-model="startTimes"
      type="date"
      placeholder="选择日期"
			value-format="yyyy-MM-dd"
      @change="val => handleChangeHandleWearTime(val, 'endTimes')"
    >
    </el-date-picker>
    <span style="margin: 0 8px;">至</span>
    <el-date-picker
      v-model="endTimes"
      type="date"
			value-format="yyyy-MM-dd"
      placeholder="选择日期">
    </el-date-picker>
  </el-row>
</template>

<script>
  import { parseTime } from "@/utils/ruoyi";
  export default{
    name:'myAutoTimePicker',
    props:{
      startTime:{
        type:String,
      },
      endTime:{
        type:String,
      },
      config: {
        type: Object,
        default: () => {
          /**
           * 根据type采用哪种自动计算后一位
           * day  天
           * month 月
           * year 年
           * custom 自定义  根据传入的fun自动运行   自定义组合计算
           */
          return {
            type: 'day',
            num: 7
          }
        }
      }
    },
    computed:{
      startTimes: {
        get(){
          return this.startTime
        },
        set(val){
          this.$emit('update:startTime', val)
        }
      },
      endTimes:{
        get(){
          return this.endTime
        },
        set(val){
          this.$emit('update:endTime',val)
        }
      }
    },
    methods: {
      handleChangeHandleWearTime(val, timeTips) {
        const { type, num, Fun } = this.config

        const date = new Date(val)
        switch (type) {
          case 'day':
            date.setDate(date.getDate() + num)
            let newDate1 = parseTime(date, "{y}-{m}-{d}")
            this.$set(this, timeTips, newDate1)
            break;
          case 'month':
            date.setMonth(date.getMonth() + num)
            let newDate2 = parseTime(date, "{y}-{m}-{d}")
            this.$set(this, timeTips, newDate2)
            break;
          case 'year':
            date.setFullYear(date.getFullYear() + num)
            date.setDate(date.getDate() - 1)
            let newDate3 = parseTime(date, "{y}-{m}-{d}")
            this.$set(this, timeTips, newDate3)
            break;
          case 'custom':
            Fun(val)
            break;
          default:
            let newDate4 = parseTime(date, "{y}-{m}-{d}")
            this.$set(this, timeTips, newDate4)
            break;
        }
      },
    }
  }
</script>

<style>
</style>
